home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Unix / MOTD / Source / MOTD.m < prev    next >
Text File  |  1995-06-12  |  5KB  |  224 lines

  1. /* Program: MOTD - 'Motto of the day' window
  2.  *
  3.  * By: Christopher Lane (lane@sumex-aim.stanford.edu)
  4.  *     Symbolic Systems Resources Group
  5.  *     Knowledge Systems Laboratory
  6.  *     Stanford University
  7.  *
  8.  * Contributors:
  9.  *
  10.  *    Izumi Ohzawa (izumi@violet.berkeley.edu)
  11.  *    Group in Neurobiology/School of Optometry
  12.  *    University of California, Berkeley
  13.  *
  14.  *    Steve Hayman (sahayman@cs.indiana.edu)
  15.  *    Computer Science Department Workstation Manager
  16.  *
  17.  * Date:  5 November 1992
  18.  *
  19.  * Copyright: 1989, 1990, 1991 & 1992 by The Leland Stanford Junior University.
  20.  * This program may be distributed without restriction for non-commercial use.
  21.  */
  22.  
  23. #import "MOTD.h"
  24. #import "DefaultsTable.h"
  25.  
  26. #define MAILSND "Mail"
  27. #define NEWMAILSND "NewMail"
  28. #define DEFAULTSFILE "Defaults"
  29.  
  30. #define MAILMESSAGE NXLocalizedString("You have mail.", NULL, "User has unread mail")
  31. #define NEWMAILMESSAGE NXLocalizedString("You have new mail.", NULL, "User has new mail")
  32. #define MAILTITLE NXLocalizedString("Mail Alert", NULL, "Mail alert panel title")
  33.  
  34. #define QUITBUTTONTITLE NXLocalizedString("Quit", NULL, "User wants to quit and proceed with login")
  35. #define HOLDBUTTONTITLE NXLocalizedString("Hold (%3d)", NULL, "User wants to hold (and format for seconds)")
  36. #define CANCELBUTTONTITLE NXLocalizedString("Cancel Login", NULL, "User wants to quit and not login")
  37.  
  38. #define SECOND ((double) 1.0)
  39.  
  40. extern void timer(DPSTimedEntry, double, id);
  41.     
  42. @implementation MOTD
  43.  
  44. + new
  45. {
  46.     char pathnamebuf[MAXPATHLEN];
  47.  
  48.     self = [super new];
  49.  
  50.     bundle = [NXBundle bundleForClass:[self class]];
  51.     
  52.     if ([bundle getPath:pathnamebuf forResource:DEFAULTSFILE ofType:"strings"])
  53.  
  54.     [[[DefaultsTable alloc] initFromFile:pathnamebuf] registerDefaults:[self appName]];
  55.     
  56.     file = getDefault("File");
  57.     
  58.     wait = getIntDefault("Wait");
  59.  
  60.     return self;
  61. }
  62.  
  63. - appDidInit:sender
  64. {
  65.     [[self appIcon] orderOut:self];
  66.  
  67.     if (loginHook) (void) [self activateSelf:YES];
  68.  
  69.     if (!do_motd) wait = getIntDefault("MailWait"); /* short timeout */  
  70.  
  71.     timedEntry = DPSAddTimedEntry(SECOND, (DPSTimedEntryProc) &timer, self, NX_BASETHRESHOLD);
  72.  
  73.     return self;
  74. }
  75.  
  76. - windowWillClose:sender { return [self terminate:sender]; }
  77.  
  78. - free
  79. {
  80.     if (timedEntry != NULL) DPSRemoveTimedEntry(timedEntry);
  81.     
  82.     return [super free];
  83. }
  84.  
  85. - setMailPanel:anObject
  86. {
  87.     char pathnamebuf[MAXPATHLEN];
  88.  
  89.     [(mailPanel = anObject) setTitle:strcat(strcpy(pathnamebuf, MAILDIR), name)];
  90.  
  91.     return self;
  92. }
  93.  
  94. - setMailText:anObject
  95. {
  96.     const char *value;
  97.     char pathnamebuf[MAXPATHLEN];
  98.  
  99.     mailText = anObject;
  100.     
  101.     if (maillevel != NONE) {
  102.         [mailText setStringValue:(maillevel == NEW) ? NEWMAILMESSAGE : MAILMESSAGE];
  103.         [[mailPanel display] orderFront:nil];
  104.         if ((value = getSystemDefault("SystemAlert")) != NULL && strcasecmp(value, "Panel") != EQ) {
  105.             if ([bundle getPath:pathnamebuf forResource:(maillevel == NEW) ? NEWMAILSND : MAILSND ofType:"snd"])
  106.                  SNDPlaySoundfile(pathnamebuf, 0);
  107.             }
  108.         }
  109.  
  110.     return self;
  111. }
  112.  
  113. - setMailTitle:anObject { [(mailTitle = anObject) setStringValue:MAILTITLE]; return self; }
  114.  
  115. - txt
  116. {
  117.     NXStream *stream;
  118.  
  119.     if ((stream = NXMapFile(file, NX_READONLY)) == NULL) return nil;
  120.     
  121.     [docView readText:stream];
  122.  
  123.     NXClose(stream);
  124.  
  125.     return self;
  126. }
  127.  
  128. - rtf
  129. {
  130.     NXStream *stream;
  131.  
  132.     if ((stream = NXMapFile(file, NX_READONLY)) == NULL) return nil;
  133.     
  134.     [docView readRichText:stream];
  135.  
  136.     NXClose(stream);
  137.  
  138.     return self;
  139. }
  140.  
  141. - rtfd
  142. {
  143.     if([docView openRTFDFrom:file] != NX_RTFDErrorNone) return nil;
  144.     
  145.     return self;
  146. }
  147.  
  148. - setWindow:anObject
  149. {
  150.     window = anObject;
  151.  
  152.     if (do_motd) [[window setTitle:file] makeKeyAndOrderFront:self];
  153.  
  154.     return self;
  155. }
  156.  
  157. - setDocView:anObject
  158. {
  159.     const char *type;
  160.     SEL method, action = @selector(txt);
  161.  
  162.     docView = anObject;
  163.     
  164.     if (!do_motd) return self;    /* Don't do it unless we have a MOTD window */
  165.  
  166.     /* Can't use [workspace getInfoForFile:file application:&application type:&type]) in LoginHook */
  167.  
  168.     if ((type = rindex(file, '.')) != NULL) {
  169.         if ((method = sel_getUid(++type)) != 0 && [self respondsTo:method]) action = method;
  170.         }
  171.         
  172.     if([self perform:action] == nil) [self terminate:self];
  173.  
  174.     return self;
  175. }
  176.  
  177. - quitButton { return quitButton; }
  178. - setQuitButton:anObject { [(quitButton = anObject) setTitle:QUITBUTTONTITLE]; return self; }
  179.  
  180. - holdButton { return holdButton; }
  181.  
  182. - setHoldButton:anObject
  183. {
  184.     [[(holdButton = anObject) setTitle:HOLDBUTTONTITLE] setState:getBoolDefault("Hold")];
  185.     
  186.     return self;
  187. }
  188.  
  189. - cancelButton { return cancelButton; }
  190. - setCancelButton:anObject { [(cancelButton = anObject) setTitle:CANCELBUTTONTITLE]; return self; }
  191.  
  192. - (const char *) userName { return name; }
  193.  
  194. - setUserName:(const char *) pw_name { name = NXCopyStringBuffer(pw_name); return self; }
  195.  
  196. - (int) UID { return uid; }
  197. - setUID:(int) pw_uid { uid = pw_uid; return self; }
  198.  
  199. - (BOOL) do_Motd { return do_motd; }
  200. - setDo_Motd:(BOOL) aBoolean { do_motd = aBoolean; return self; }
  201.  
  202. - (MailLevel) mailLevel { return maillevel; }
  203. - setMailLevel:(MailLevel) aMailLevel { maillevel = aMailLevel; return self; }
  204.  
  205. - (const char *) file { return file; }
  206. - setFile:(const char *) aString { file = aString; return self; }
  207.  
  208. - (BOOL) loginHook { return loginHook; }
  209. - setLoginHook:(BOOL) aBoolean { loginHook = aBoolean; return self; }
  210.  
  211. - (int) wait { return wait; }
  212. - setWait:(int) anInteger { wait = anInteger; return self; }
  213.  
  214. - cancelLogin:sender
  215. {
  216.     [self free];
  217.  
  218.     exit(EXIT_FAILURE);
  219.     
  220.     return self; /* NOT REACHED */
  221. }
  222.  
  223. @end
  224.